﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using Automobile.service.Model;

namespace Automobile.service.DAL
{
    public class Document_MateDal
    {
        string str = "Data Source=.;Initial Catalog=qichefuwu;Integrated Security=True";

        /// <summary>
        /// 显示商品库存信息
        /// </summary>
        /// <returns></returns>
        public List<DocumentModel> Document()
        {
            string sql = "select Document_Id,Document_Type_Name,WareHose_Id,Document_Date,Employee_Id,Document_Remark from Document";
            SqlDataAdapter adapter = new SqlDataAdapter(sql, str);
            DataTable table = new DataTable();
            adapter.Fill(table);
            List<DocumentModel> list = new List<DocumentModel>();
            foreach (DataRow item in table.Rows)
            {
                DocumentModel s = new DocumentModel();
                s.Document_Id = Convert.ToInt32(item["Document_Id"]);
                s.Document_Type_Name = item["Document_Type_Name"].ToString();
                s.WareHose_Id = Convert.ToInt32(item["WareHose_Id"]);
                s.Document_Date = Convert.ToDateTime(item["Document_Date"]);
                s.Employee_Id = Convert.ToInt32(item["Employee_Id"]);
                s.Document_Remark = item["Document_Remark"].ToString();
                list.Add(s);
            }
            return list;
        }
        /// <summary>
        /// 添加商品库存信息
        /// </summary>
        /// <returns></returns>
        public int AddDocument(DocumentModel s)
        {
            string sql = "insert into Document(Document_Type_Name,WareHose_Id,Document_Date,Employee_Id,Document_Remark) values('" + s.Document_Type_Name + "','" + s.WareHose_Id + "','" + s.Document_Date + "','" + s.Employee_Id + "','" + s.Document_Remark + "')";
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            int i = comm.ExecuteNonQuery();
            conn.Close();
            return i;
        }
        /// <summary>
        /// 删除商品库存信息
        /// </summary>
        /// <returns></returns>
        public int DeleDocument(int Document_Id)
        {
            string sql = "delete from Document where Document_Id=" + Document_Id;
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            int i = comm.ExecuteNonQuery();
            conn.Close();
            return i;
        }

        /// <summary>
        /// 取单个商品库存信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetDocument(string Document_Id)
        {
            string sql = "select Document_Id,Document_Type_Name,WareHose_Id,Document_Date,Employee_Id,Document_Remark from Document where Document_Id=" + Document_Id;

            SqlConnection conn = new SqlConnection(str);
            conn.Open();//打开数据库

            SqlCommand comm = new SqlCommand(sql, conn);
            DataTable table = new DataTable();
            SqlDataAdapter ada = new SqlDataAdapter(comm);
            ada.Fill(table);
            conn.Close();
            return table;
        }

        /// <summary>
        /// 更新商品库存信息
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        public int UpdateDocument(DocumentModel s)
        {
            string sql = "update Document set Document_Type_Name='" + s.Document_Type_Name + "',WareHose_Id='" + s.WareHose_Id + "',Document_Date='" + s.Document_Date + "',Employee_Id='" + s.Employee_Id + "',Document_Remark='" + s.Document_Remark + "' where Document_Id=" + s.Document_Id;
            SqlConnection conn = new SqlConnection(str);
            conn.Open();//打开数据库

            SqlCommand comm = new SqlCommand(sql, conn);
            //执行命令
            int count = comm.ExecuteNonQuery();
            conn.Close();

            return count;
        }
    }
}
